home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FLEX-TC_ / YYLEX.C < prev   
Text File  |  1990-01-02  |  4KB  |  241 lines

  1. /* yylex - scanner front-end for flex */
  2.  
  3. /*
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant to
  11.  * contract no. DE-AC03-76SF00098 between the United States Department of
  12.  * Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted
  15.  * provided that the above copyright notice and this paragraph are
  16.  * duplicated in all such forms and that any documentation,
  17.  * advertising materials, and other materials related to such
  18.  * distribution and use acknowledge that the software was developed
  19.  * by the University of California, Berkeley.  The name of the
  20.  * University may not be used to endorse or promote products derived
  21.  * from this software without specific prior written permission.
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  23.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25.  */
  26.  
  27. #ifndef lint
  28.  
  29. static char copyright[] =
  30.     "@(#) Copyright (c) 1989 The Regents of the University of California.\n";
  31. static char CR_continuation[] = "@(#) All rights reserved.\n";
  32.  
  33. static char rcsid[] =
  34.     "@(#) $Header: yylex.c,v 2.0 89/06/20 15:50:28 vern Locked $ (LBL)";
  35.  
  36. #endif
  37.  
  38. #include "flexdef.h"
  39. #include "parse.h"
  40.  
  41. /* yylex - scan for a regular expression token
  42.  *
  43.  * synopsis
  44.  *
  45.  *   token = yylex();
  46.  *
  47.  *     token - return token found
  48.  */
  49.  
  50. int yylex()
  51.  
  52.     {
  53.     int toktype;
  54.     static int beglin = false;
  55.  
  56.     if ( eofseen )
  57.     toktype = EOF;
  58.     else
  59.     toktype = flexscan();
  60.  
  61.     if ( toktype == EOF )
  62.     {
  63.     eofseen = 1;
  64.  
  65.     if ( sectnum == 1 )
  66.         {
  67.         synerr( "unexpected EOF" );
  68.         sectnum = 2;
  69.         toktype = SECTEND;
  70.         }
  71.  
  72.     else if ( sectnum == 2 )
  73.         {
  74.         sectnum = 3;
  75.         toktype = SECTEND;
  76.         }
  77.  
  78.     else
  79.         toktype = 0;
  80.     }
  81.  
  82.     if ( trace )
  83.     {
  84.     if ( beglin )
  85.         {
  86.         fprintf( stderr, "%d\t", num_rules + 1 );
  87.         beglin = 0;
  88.         }
  89.  
  90.     switch ( toktype )
  91.         {
  92.         case '<':
  93.         case '>':
  94.         case '^':
  95.         case '$':
  96.         case '"':
  97.         case '[':
  98.         case ']':
  99.         case '{':
  100.         case '}':
  101.         case '|':
  102.         case '(':
  103.         case ')':
  104.         case '-':
  105.         case '/':
  106.         case '\\':
  107.         case '?':
  108.         case '.':
  109.         case '*':
  110.         case '+':
  111.         case ',':
  112.         (void) putc( toktype, stderr );
  113.         break;
  114.  
  115.         case '\n':
  116.         (void) putc( '\n', stderr );
  117.  
  118.         if ( sectnum == 2 )
  119.             beglin = 1;
  120.  
  121.         break;
  122.  
  123.         case SCDECL:
  124.         fputs( "%s", stderr );
  125.         break;
  126.  
  127.         case XSCDECL:
  128.         fputs( "%x", stderr );
  129.         break;
  130.  
  131.         case WHITESPACE:
  132.         (void) putc( ' ', stderr );
  133.         break;
  134.  
  135.         case SECTEND:
  136.         fputs( "%%\n", stderr );
  137.  
  138.         /* we set beglin to be true so we'll start
  139.          * writing out numbers as we echo rules.  flexscan() has
  140.          * already assigned sectnum
  141.          */
  142.  
  143.         if ( sectnum == 2 )
  144.             beglin = 1;
  145.  
  146.         break;
  147.  
  148.         case NAME:
  149.         fprintf( stderr, "'%s'", nmstr );
  150.         break;
  151.  
  152.         case CHAR:
  153.         switch ( yylval )
  154.             {
  155.             case '<':
  156.             case '>':
  157.             case '^':
  158.             case '$':
  159.             case '"':
  160.             case '[':
  161.             case ']':
  162.             case '{':
  163.             case '}':
  164.             case '|':
  165.             case '(':
  166.             case ')':
  167.             case '-':
  168.             case '/':
  169.             case '\\':
  170.             case '?':
  171.             case '.':
  172.             case '*':
  173.             case '+':
  174.             case ',':
  175.             fprintf( stderr, "\\%c", yylval );
  176.             break;
  177.  
  178.             case 1:
  179.             case 2:
  180.             case 3:
  181.             case 4:
  182.             case 5:
  183.             case 6:
  184.             case 7:
  185.             case 8:
  186.             case 9:
  187.             case 10:
  188.             case 11:
  189.             case 12:
  190.             case 13:
  191.             case 14:
  192.             case 15:
  193.             case 16:
  194.             case 17:
  195.             case 18:
  196.             case 19:
  197.             case 20:
  198.             case 21:
  199.             case 22:
  200.             case 23:
  201.             case 24:
  202.             case 25:
  203.             case 26:
  204.             case 27:
  205.             case 28:
  206.             case 29:
  207.             case 30:
  208.             case 31:
  209.             case 127:
  210.             fprintf( stderr, "\\%.3o", yylval );
  211.             break;
  212.  
  213.             default:
  214.             (void) putc( yylval, stderr );
  215.             break;
  216.             }
  217.             
  218.         break;
  219.  
  220.         case NUMBER:
  221.         fprintf( stderr, "%d", yylval );
  222.         break;
  223.  
  224.         case PREVCCL:
  225.         fprintf( stderr, "[%d]", yylval );
  226.         break;
  227.  
  228.         case 0:
  229.         fprintf( stderr, "End Marker" );
  230.         break;
  231.  
  232.         default:
  233.         fprintf( stderr, "*Something Weird* - tok: %d val: %d\n",
  234.              toktype, yylval );
  235.         break;
  236.         }
  237.     }
  238.         
  239.     return ( toktype );
  240.     }
  241.